package com.rural.bus.driver.model;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.rural.bus.driver.MyApplication;
import com.rural.bus.driver.net.MyHttpUtil;
import com.rural.bus.driver.net.OnNetCallback;
import com.rural.bus.driver.net.Url;

/***
 * 
 * 行程
 * @author majie
 *
 */
public class Railway {
	
	public Order order;
	public int callStatus;
	
	/***
	 * accept
	 */
	public void accept(final Context context,int uid ,int orderId,int driverId,int carType,int orderType,int number,final OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("uid", ""+uid);
		http.addBodyParm("orderId", ""+orderId);
		http.addBodyParm("driverId", ""+driverId);
		http.addBodyParm("carType", ""+carType);
		http.addBodyParm("orderType", ""+orderType);
		http.addBodyParm("number", ""+number);
		
		http.send(Url.accept_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
			    parse(context,object);
				callback.success(object);
				
			}
			
			@Override
			public void fail(String error) {
				callback.fail(error);
			}
		});
	}
	
	private void parse(Context context,JSONObject object){
		  try {
				Railway.this.order=Order.parse(object.getJSONObject("order"));
				Railway.this.callStatus=object.getInt("callStatus");
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
	}
	
	/***
	 * railway ready
	 */
	public void ready(final Context context,int orderId,int groupId,int driverId,final OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("orderId", ""+orderId);
		http.addBodyParm("groupId", ""+groupId);
		http.addBodyParm("driverId", ""+driverId);
        http.send(Url.ready_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				parse(context, object);
				callback.success(object);
			}
			
			@Override
			public void fail(String error) {
				callback.fail(error);
			}
		});
	}
	
	/***
	 * railway ready
	 */
	public void start(final Context context,int orderId,int groupId,int driverId,final OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("orderId", ""+orderId);
		http.addBodyParm("groupId", ""+groupId);
		http.addBodyParm("driverId", ""+driverId);
        http.send(Url.start_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				parse(context, object);
				callback.success(object);
			}
			
			@Override
			public void fail(String error) {
				callback.fail(error);
			}
		});
	}
	
	/***
	 * railway confirm
	 */
	public void confirm(final Context context,int orderId,int groupId,int driverId,double distance,final OnNetCallback callback){
		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("orderId", ""+orderId);
		http.addBodyParm("groupId", ""+groupId);
		http.addBodyParm("driverId", ""+driverId);
		http.addBodyParm("distance", ""+distance);

        http.send(Url.confirm_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				parse(context, object);
				callback.success(object);
			}
			
			@Override
			public void fail(String error) {
				callback.fail(error);
			}
		});
	}

}
